package cn.com.aiouyi.controller.web;

import java.util.Date;

import org.slf4j.Logger;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.aiouyi.common.DateEditor;
import cn.com.aiouyi.common.Message;
import lombok.extern.slf4j.Slf4j;

/**
 * 基类
 * @author hanson
 *
 */
@Slf4j
public abstract class BaseController {
	
	protected final Logger logger = log;
    
	/** 错误视图 */
	protected static final String ERROR_VIEW = "/admin/common/error";

	
	/** 前端显示页面错误信息key */
	public static final String ERROR_VIEW_MESSAGE_KEY = "errorMessage";
	
    /**
	 * 数据绑定
	 * @param binder WebDataBinder
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		binder.registerCustomEditor(Date.class, new DateEditor(true));
	}

	/**
	 * 添加瞬时消息
	 * @param redirectAttributes
	 * @param message 消息
	 */
	protected void addFlashMessage(RedirectAttributes redirectAttributes, Message message) {
		if (redirectAttributes != null && message != null) {
			redirectAttributes.addFlashAttribute("flashMessage", message);
		}
	}

}
